/**
 * 报表设计公司(2011)
 */
package com.reportgear.report.core.api.idname;

import com.reportgear.report.core.api.compare.ComparableInterface;
import com.reportgear.report.core.api.compare.ComparatorUtils;

/**
 * 默认包含Id与Name的对象
 * 
 * @version 1.0 2011-3-9
 * @author <a herf="lexloo@gmail.com">lexloo</a>
 * @since ANNCSR 2.0
 */
public class DefaultIdName<T> implements IdName<T>, ComparableInterface<IdName<T>> {
	private T id;
	private String name;

	/**
	 * 构造器
	 * 
	 * @param id
	 *            id
	 * @param name
	 *            名称
	 */
	public DefaultIdName(T id, String name) {
		this.id = id;
		this.name = name;
	}

	@Override
	public T getId() {
		return id;
	}

	@Override
	public String toString() {
		return this.name;
	}

	/**
	 * 设置Id
	 * 
	 * @param id
	 *            id
	 */
	public void setId(T id) {
		this.id = id;
	}

	@Override
	public String getName() {
		return name;
	}

	/**
	 * 设置名称
	 * 
	 * @param name
	 *            名称
	 */
	public void setName(String name) {
		this.name = name;
	}

	@Override
	public int compareTo(IdName<T> o) {
		return ComparatorUtils.compare(this, o);
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((id == null) ? 0 : id.hashCode());
		result = prime * result + ((name == null) ? 0 : name.hashCode());
		return result;
	}

	@SuppressWarnings("unchecked")
	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		DefaultIdName other = (DefaultIdName) obj;
		if (id == null) {
			if (other.id != null)
				return false;
		} else if (!id.equals(other.id))
			return false;
		if (name == null) {
			if (other.name != null)
				return false;
		} else if (!name.equals(other.name))
			return false;
		return true;
	}
}
